home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Development Kits / Mac OS / USB DDK 1.4.6f4 / Examples / USBEnetSample / USBEnetStub.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-23  |  10.2 KB  |  350 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        USBEnetStub.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1998-2000 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                xxx put dri here xxx
  13.  
  14.         Other Contact:        xxx put other contact here xxx
  15.  
  16.         Technology:            xxx put technology here xxx
  17.  
  18. */
  19.  
  20. #include <Errors.h>
  21. #include <CodeFragments.h>
  22.  
  23. #include "USBEnet.h"
  24. #include "USBEnetStub.h"
  25. #include "USBEnetDriver.h"
  26. #include "ShimEnetStub.h"
  27. #include "ShimEnetHAL.h"
  28. #include "USBEnetVersion.h"
  29.  
  30. //------------------------------------------------------
  31. //
  32. // Globals
  33. //
  34. //------------------------------------------------------
  35.  
  36. ShimEnetGlobals*    gGlobals;
  37.  
  38. //------------------------------------------------------
  39. //
  40. //    This is the driver description structure that the expert looks for first.
  41. //  If it's here, the information within is used to match the driver
  42. //  to the device whose descriptor was passed to the expert.
  43. //    Information in this block is also used by the expert when an
  44. //  entry is created in the Name Registry.
  45. //
  46. //------------------------------------------------------
  47.  
  48. USBDriverDescription    TheUSBDriverDescription = 
  49. {
  50.     // Signature info
  51.     kTheUSBDriverDescriptionSignature,
  52.     kInitialUSBDriverDescriptor,
  53.     
  54.     // Device Info
  55.     kUSBVendorID,                             // vendor ID
  56.     kUSBProductID,                            // product ID
  57.     0,                                        // version of product = not device specific
  58.     0,                                        // protocol = not device specific
  59.     
  60.     // Interface Info            
  61.     0,                                        // Configuration Value (doesn't matter)
  62.     0,                                        // Interface Number
  63.     kUSBCommClass,                            // Interface Class
  64.     6,                                         // Interface SubClass (Ethernet Control Model)
  65.     0,                                        // Interface Protocol (none)    
  66.     
  67.     // Driver Info
  68.     kUSBRegName,                            // Driver name for Name Registry
  69.     kUSBCommClass,                            // Device Class
  70.     0,                                        // Device Subclass 
  71.     kVMajor,                                 // version of driver (see USBEnetVersion.h)
  72.     kVMinor, 
  73.     kVStage, 
  74.     kVRelease,
  75.     
  76.     // Driver Loading Info
  77. //    kUSBDoNotMatchGenericDevice                // Flags
  78.     0
  79. };
  80.     
  81. USBClassDriverPluginDispatchTable TheClassDriverPluginDispatchTable =
  82. {
  83.     kClassDriverPluginVersion,                // Version of this structure
  84.     USBEnetDriverValidateHW,                // Hardware Validation Procedure
  85.     USBEnetDriverInitialize,                // Initialization Procedure
  86.     USBEnetDriverInitInterface,                // Interface Initialization Procedure
  87.     USBEnetDriverFinalize,                    // Finalization Procedure
  88.     USBEnetDriverNotifyProc,
  89. };
  90.  
  91. /***********************************************************************************/
  92. //    Function:        BuildName(USBRef)
  93. //    Description:    This routine uses the name registry LocationID to construct
  94. //                    the devices real name
  95. //
  96. //    Input:            USBRef - USB Device Ref, reftype - true (Device), False (interface)
  97. //    Output:            
  98. /***********************************************************************************/
  99. static void BuildName(USBDeviceRef USBRef, Boolean reftype)
  100. {
  101.     OSStatus                 err = noErr;
  102.     RegEntryIter            cookie;
  103.     RegEntryID                theID;
  104.     Boolean                    done;
  105.     UInt32                    Rlocation;
  106.     UInt8                    *location = (unsigned char *)&Rlocation;
  107.     RegPropertyValueSize    theSize;
  108.     UInt16                    offs, i;
  109.     UInt8                    asctmp = 0;
  110.     
  111.     TraceMessage(0, kDrvName"- Entering BuildName");
  112.     
  113.     err = RegistryEntryIDInit(&theID);
  114.     if (err != noErr)
  115.     {
  116.         StatusMessage(USBRef, kDrvName"- BuildName: RegistryEntryIDInit failed - ", err);
  117.         return;
  118.     }
  119.         
  120.     err = RegistryEntryIterateCreate(&cookie);
  121.     if (err != noErr)
  122.     {
  123.         StatusMessage(USBRef, kDrvName"- BuildName: RegistryEntryIterateCreate failed - ", err);
  124.         return;
  125.     }
  126.         
  127.     if (reftype)
  128.     {
  129.         StatusMessage(USBRef, kDrvName"- BuildName: Using Device Ref - ", USBRef);
  130.         err = RegistryEntrySearch(&cookie, kRegIterDescendants, &theID, &done, "deviceRef", (const void *)&USBRef, (RegPropertyValueSize) sizeof(USBRef));
  131.     } else {
  132.         StatusMessage(USBRef, kDrvName"- BuildName: Using Interface Ref - ", USBRef);
  133.         err = RegistryEntrySearch(&cookie, kRegIterDescendants, &theID, &done, "interfaceRef", (const void *)&USBRef, (RegPropertyValueSize) sizeof(USBRef));
  134.     }
  135.     
  136.     if (err != noErr)
  137.     {
  138.         StatusMessage(USBRef, kDrvName"- BuildName: RegistryEntrySearch failed - ", err);
  139.         RegistryEntryIterateDispose(&cookie);
  140.         return;
  141.     }
  142.     
  143.     theSize = sizeof(Rlocation);
  144.     err = RegistryPropertyGet(&theID, "locationID", &Rlocation, &theSize);
  145.     if (err != noErr)
  146.     {
  147.         StatusMessage(USBRef, kDrvName"- BuildName: RegistryPropertyGet failed - ", err);
  148.         RegistryEntryIterateDispose(&cookie);
  149.         return;
  150.     }
  151.     
  152.     gGlobals->theID = theID;                    // save our reg entry    
  153.     offs = (gGlobals->rname[0] + 1);
  154.     for (i=1; i<=theSize; i++)
  155.     {
  156.         asctmp = (location[i-1] >> 4);
  157.         if (asctmp != 0)
  158.             gGlobals->rname[offs++] = Asciify(location[i-1] >> 4);
  159.             
  160.         asctmp = (location[i-1] & 0x0F);
  161.         if (asctmp != 0)
  162.             gGlobals->rname[offs++] = Asciify(location[i-1]);
  163.             
  164.         asctmp = 0;
  165.     }
  166.     gGlobals->rname[0] = offs;
  167.     
  168.     RegistryEntryIterateDispose(&cookie);
  169. }
  170.  
  171. /***********************************************************************************/
  172. //    Function:        USBEnetDriverValidateHW(USBDeviceRef device, USBDeviceDescriptor *desc)
  173. //    Description:    Called upon load by Expert
  174. //
  175. //    Input:            Device reference, Device descriptor
  176. //    Output:            noErr
  177. /***********************************************************************************/
  178.  
  179. static OSStatus USBEnetDriverValidateHW(USBDeviceRef device, USBDeviceDescriptor *desc)
  180. {
  181. #pragma unused (device, desc)
  182.  
  183.     TraceMessage(0,kDrvName"- Entering USBEnetDriverValidateHW");
  184.         
  185.     return noErr;
  186. }
  187.  
  188. /***********************************************************************************/
  189. //    Function:        OSStatus USBEnetDriverInitialize(USBDeviceRef device, USBDeviceDescriptorPtr pDesc,
  190. //                                                                         UInt32 busPowerAvailable)
  191. //    Description:    Called upon load by Expert
  192. //
  193. //    Input:            Device reference, Device descriptor, bus power available
  194. //    Output:            result
  195. /***********************************************************************************/
  196.  
  197. static OSStatus USBEnetDriverInitialize(USBDeviceRef device, USBDeviceDescriptorPtr pDesc, UInt32 busPowerAvailable)
  198. {
  199. #pragma unused (busPowerAvailable)
  200.     
  201. OSStatus err;
  202.  
  203.     TraceMessage(0,kDrvName"- Entering USBEnetDriverInitialize");
  204.  
  205.     gGlobals = (ShimEnetGlobals*)PoolAllocateResident(sizeof(ShimEnetGlobals),true);
  206.     if (gGlobals == NULL)
  207.         goto bail;
  208.     
  209.     USBExpertStatus(0, kDrvName"- USBEnetDriverInitialize: Loading as Class - "kVers2Long Bugon, 0);
  210.     
  211.     // Could use the Product string but for now we don't
  212.     BlockMoveData(kDrvName, &gGlobals->rname, sizeof(kDrvName));
  213.     BuildName(device, true);
  214.     err = USBEnetDriverEntry(device, pDesc);
  215.  
  216.     if (err != noErr)
  217.     {
  218.         StatusMessage(0, kDrvName"- USBEnetDriverInitialize: Configuration failed", err);
  219.         goto bail;
  220.     } 
  221.     return noErr;
  222.  
  223. bail:
  224.     if (gGlobals)
  225.         PoolDeallocate(gGlobals);
  226.     gGlobals = NULL;
  227.     
  228.     return openErr;
  229. }
  230.  
  231. /***********************************************************************************/
  232. //    Function:        USBEnetDriverInitInterface(UInt32 interfaceNum, USBInterfaceDescriptor *interfaceDesc, 
  233. //                                                    USBDeviceDescriptor    *deviceDesc, USBDeviceRef device)
  234. //    Description:    Called to initialize driver for an individual interface - 
  235. //                    either by expert or internally by driver
  236. //
  237. //    Input:            Interface number, Interface descriptor, Device Descriptor, Device Reference
  238. //    Output:            result
  239. /***********************************************************************************/
  240.  
  241. static OSStatus USBEnetDriverInitInterface(UInt32 interfaceNum, USBInterfaceDescriptor *interfaceDesc, USBDeviceDescriptor *deviceDesc, USBDeviceRef device)
  242. {
  243. #pragma unused (interfaceNum, interfaceDesc)
  244.  
  245. OSStatus err;
  246.  
  247.     TraceMessage(0,kDrvName"- Entering USBEnetDriverInitInterface");
  248.  
  249.     if (gGlobals == NULL)
  250.         gGlobals = (ShimEnetGlobals*)PoolAllocateResident(sizeof(ShimEnetGlobals),true);
  251.  
  252.     if (gGlobals == NULL)
  253.         goto bail;
  254.  
  255.     USBExpertStatus(0, kDrvName"- USBEnetDriverInitInterface: Loading as Interface "kVers2Long Bugon, 0);
  256.     
  257.     // Could use the Product string but for now we don't
  258.     BlockMoveData(kDrvName, &gGlobals->rname, sizeof(kDrvName));
  259.     BuildName(device, false);
  260.     err = USBEnetDriverEntry(device, deviceDesc);
  261.  
  262.     if (err != noErr)
  263.     {
  264.         StatusMessage(0, kDrvName"- USBEnetDriverInitInterface: Configuration failed", err);
  265.         goto bail;
  266.     }
  267.         
  268.     return noErr;
  269.  
  270. bail:
  271.     if (gGlobals)
  272.         PoolDeallocate(gGlobals);
  273.     gGlobals = NULL;
  274.     
  275.     return openErr;
  276. }
  277.  
  278. /***********************************************************************************/
  279. //    Function:        USBEnetDriverFinalize(USBDeviceRef device, USBDeviceDescriptorPtr pDesc)
  280. //    Description:    Called by Expert when driver is being shut down
  281. //
  282. //    Input:            Device reference, Device descriptor
  283. //    Output:            noErr
  284. /***********************************************************************************/
  285.  
  286. static OSStatus USBEnetDriverFinalize(USBDeviceRef device, USBDeviceDescriptorPtr pDesc)
  287. {
  288. #pragma unused (device, pDesc)
  289.  
  290.     TraceMessage(0,kDrvName"- Entering USBEnetDriverFinalize");
  291.     
  292.     if (gGlobals)
  293.     {
  294.         /* Now we can kill all the IO */
  295.         KillUSBIO();
  296.         RemoveShimDrvr(true);                            // force close - let the shim handle it
  297.         PoolDeallocate(gGlobals);
  298.         gGlobals = NULL;
  299.     }
  300.     
  301.     return noErr;
  302. }
  303.  
  304. /***********************************************************************************/
  305. //    Function:        ExpertEntryProc(ExpertNotificationProcPtr pExpertNotify)
  306. //    Description:    
  307. //
  308. //    Input:            Notification proc pointer
  309. //    Output:            noErr
  310. /***********************************************************************************/
  311.  
  312. static OSStatus ExpertEntryProc(ExpertNotificationProcPtr pExpertNotify)
  313. {
  314. #pragma unused (pExpertNotify)
  315.  
  316.     TraceMessage(0,kDrvName"- Entering ExpertEntryProc");
  317.  
  318.     return noErr;
  319. }
  320.  
  321. /***********************************************************************************/
  322. //    Function:        USBEnetDriverNotifyProc(UInt32 notification, void *pointer)
  323. //    Description:    
  324. //
  325. //    Input:            Notification and pointer
  326. //    Output:            result
  327. /***********************************************************************************/
  328.  
  329. static OSStatus    USBEnetDriverNotifyProc(UInt32 notification, void *pointer, UInt32 refcon)
  330. {
  331. #pragma unused (pointer, refcon)
  332.  
  333.     OSErr    err = noErr;
  334.     
  335.     TraceMessage(0,kDrvName"- Entering USBEnetDriverNotifyProc");
  336.     StatusMessage(0, kDrvName"- USBEnetDriverNotifyProc: Notification = ", notification);
  337.     
  338.     if (notification == kNotifySystemSleepRequest)
  339.     {
  340.         if (gGlobals)
  341.         {
  342.             if (gGlobals->openSession)                        // Look to see if we're selected
  343.             {
  344.                 err = -1;                                    // Defer for now - it's only a request
  345.             }
  346.         } 
  347.     }
  348.     
  349.     return err;
  350. }